package com.iter.devbox.annotation;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileProcessor {

	public static void main(String[] args) throws Throwable {
		Class clazz = Class.forName("com.iter.devbox.annotation.MyFile");
		
		Field field = clazz.getDeclaredField("filePath");
		FilePath filePath = field.getAnnotation(FilePath.class);
		//得到注解上配置的具体的值，这个值可以供我们程序使用
		System.out.println(filePath.value());
		
		//如果没有处理程序，直接使用将无法获取到值
		MyFile myFile = new MyFile();
		System.out.println(myFile.getFilePath());
		
		//Class clazz = Class.forName("com.iter.devbox.annotation.MyFile");
		Method method = clazz.getDeclaredMethod("copyFile", null);
		CopyFile copyFile = method.getAnnotation(CopyFile.class);
		System.out.println(copyFile.fileSrc());
		System.out.println(copyFile.fileDest());
		
		FileInputStream fis = new FileInputStream(copyFile.fileSrc());
		FileChannel inChannel = fis.getChannel();
		ByteBuffer inByteBuffer = ByteBuffer.allocate(1024);
		//在inChannel向buffer读数据之前,使用clear();
		inByteBuffer.clear();
		int rSize = inChannel.read(inByteBuffer);
		System.out.println(rSize);
		
		FileOutputStream fos = new FileOutputStream(copyFile.fileDest());
		FileChannel outChannel = fos.getChannel();
		//在buffer向outChannel写数据之前,使用flip;
		inByteBuffer.flip();
		int wSize = outChannel.write(inByteBuffer);
		System.out.println(wSize);
		
		fis.close();
		inChannel.close();
		fos.close();
		outChannel.close();
	}
}
